const gulp = require('gulp');
const rename = require('gulp-rename');
const less = require('gulp-less');

function swallowError(error) {
    console.error(error.toString());
    this.emit('end')
}

gulp.task('less', function () {
    return gulp.src('./code/less/*.less')
        .pipe(less())
        .on('error', swallowError)
        .pipe(rename(path => {
            path.extname = '.wxss'
        }))
        .pipe(gulp.dest('./code/less'));
});

gulp.task('watch', ['less'], function () {
    // 监听变化文件
    const watch_files = [
        './code/less',
        './code/less/main.less',
        './code/less/**/*.less',
    ];
    // 变化时执行的方法
    const watch_methods = ['less'];

    gulp.watch(watch_files, watch_methods);
});
